package moe.oil.db.entity;

import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import moe.oil.dto.feedback.FeedbackDTO;
import moe.oil.entity.BaseEntity;
import moe.oil.enumeration.FeedBackType;
import org.springframework.data.annotation.CreatedDate;

import javax.persistence.*;
import java.time.LocalDate;

/**
 * 反馈
 */

@Entity

@Table
@Data
@Setter
@Getter
@NoArgsConstructor
@Accessors(chain = true)
public class Feedback implements BaseEntity<FeedbackDTO> {
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "feedback_seq")
    private Long feedbackId;

    @Column(name = "content")
    private String content;

    @JoinColumn(name = "user_id")
    @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private User user;

    @Column(name = "status")
    private FeedBackType status;

    @CreatedDate
    @Column(name = "create_time")
    private LocalDate createTime;

    @Column(name = "like_count", columnDefinition = "int default 0")
    private Long likeCount;

    @Column(name = "browse_count", columnDefinition = "int default 0")
    private Long browseCount;


    public Feedback(FeedbackDTO feedbackDTO) {
        this.content = feedbackDTO.getContent();
        this.user = new User(feedbackDTO.getUserDTO());
        this.status = feedbackDTO.getStatus();
        this.createTime = feedbackDTO.getCreateTime();
        this.likeCount = feedbackDTO.getLikeCount();
        this.browseCount = feedbackDTO.getBrowseCount();
        this.feedbackId = feedbackDTO.getFeedbackId();
    }


    public Feedback setStatus(FeedBackType status) {
        if (status == null) {
            this.status = FeedBackType.UNKNOWN;
        }
        this.status = status;
        return this;
    }

    @Override
    public FeedbackDTO convert() {
        return new FeedbackDTO()
                .setCreateTime(createTime)
                .setContent(content)
                .setFeedbackId(feedbackId)
                .setStatus(status)
                .setFeedbackId(feedbackId)
                .setLikeCount(likeCount)
                .setBrowseCount(browseCount);

    }

    @Override
    public String toString() {
        return String.format(
                "FeedBack{feedbackId=%s,content=%s,userId=%s," +
                        "status=%s,createTime=%s,likeCount=%s,browseCount=%d}",
                feedbackId, content, user, status, createTime, likeCount, browseCount);
    }
}
